package com.woniu.teachmall04.service;

import com.woniu.teachmall04.dto.CarItem;
import com.woniu.teachmall04.dto.OrderItem;
import com.woniu.teachmall04.entity.Address;
import com.woniu.teachmall04.entity.Goods;
import com.woniu.teachmall04.entity.Order;
import com.woniu.teachmall04.mapper.OrderMapper;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class OrderService {
    @Autowired
    ShopcarService shopcarService;
    @Autowired
    AddressService addressService;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    private GoodsService goodsService;

    //生成订单
    public String submitOrder(Integer userId, Integer addressId, String telphone, String payType, HttpSession session) {
        //1.获取购物车数据
        List<CarItem> cartItems = shopcarService.getCar(userId);
        if (cartItems == null || cartItems.isEmpty()) return null;
        //获取地址信息
        Address address = addressService.getAddressById(addressId);
        if (address == null) return null;

        // 1. 生成订单编号
        String orderNo = userId + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        Double totalMoney = shopcarService.getTotalPrice(userId);
        // 2. 插入wn_order
        Order order = new Order();
        order.setNo(orderNo);
        order.setAddTime(new Date());
        order.setUserId(userId);
        order.setTotalMoney(totalMoney);
        order.setAddress(address.getProvince() + address.getCity() + address.getArea() + address.getStreet());
        order.setAccept(address.getAccept());
        order.setTelphone(telphone);
        order.setPayType(payType);
        order.setStatus("0"); // 未支付
        orderMapper.insertOrder(order); // 需返回自增id

        // 3. 插入wn_order_item
        for (CarItem item : cartItems) {
            OrderItem orderItem = new OrderItem();
            orderItem.setOrderId(order.getId());
            orderItem.setGoodsId(item.getGoodid());
            orderItem.setName(item.getGoodname());
            orderItem.setImage(item.getGoodimage());
            orderItem.setPrice(item.getGoodprice());
            orderItem.setNum(item.getNum());
            orderMapper.insertOrderItem(orderItem);
        }
        return orderNo;
    }
    //支付订单
    /*
    * - 1.判断是否登录
- 2.购物车是否有数据
- 3.每个商品是否有库存
- 4.减少商品库存
- 5.清理购物车
- 6.更新订单状态为已支付
    * */
    @Transactional
    public String payOrder(Integer userId, String orderId){
        //1.购物车里是否有数据
        List<CarItem> cartItems = shopcarService.getCar(userId);
        if (cartItems == null || cartItems.isEmpty()) return null;
        //2.判断库存
        for(CarItem item : cartItems){
            //判断库存
            Goods good = goodsService.findById(item.getGoodid());
            if(item.getNum() > good.getStock()){
                return  "fail:stock";
            }
        }
        //3. 修改商品库存
        for (CarItem item : cartItems) {
            goodsService.reduceStock(item.getGoodid(), item.getNum());
        }
        //清理购物车
        shopcarService.clearCar(userId);
        // 更新订单状态
        this.updateOrderStatus(orderId, "3", new Date());
        return "success";
     }

    private void updateOrderStatus(String orderId, String status, Date date) {
        orderMapper.updateOrderStatus(orderId,status,date);
    }
    //获取某个人的所有订单
    public List<OrderItem> getOrder(Integer userId) {
        return  orderMapper.getOrderList(userId);
    }
}
